//初始化组件
function initModule(packageID){
	$("#areaEdit").window({
		title: '包适用地区设置',
		width: 688,
		height:460,
		modal: true,
		shadow: false,
		closed: true,
		resizable : false,
		draggable : true,
		minimizable : false,
		maximizable : false
	});

	$("#channelEdit").window({
		title: '包发布渠道设置',
		width: 680,
		height:460,
		modal: true,
		shadow: false,
		closed: true,
		resizable : false,
		draggable : true,
		minimizable : false,
		maximizable : false
	});

	//初始化包已发的信息。
loadPubArea(packageID);
loadPubChannel(packageID);

}

function loadPubArea(packageID){

	var param = "packageId="+packageId;
	$.ajax({
		type:"POST",
		url:"../json/getPackage2Area.action?"+param,
		dataType:"json",
		async:false,
		success:function(data){

			 var areaIds='';
			 var areaNames='';
			 var a = data;
				for(var i = 0; i < a.length; i++){
					areaIds += a[i].areaId;
					areaNames += a[i].name;
					if(i != (a.length-1)){
						areaIds += ',';
						areaNames += ',';
					}
				}
				var areaDisplay = $("#packageArea").val();

				var publishAreaIds = $("#publishArea").val();
				if(areaDisplay == '' ||areaDisplay == undefined){
					areaDisplay = areaNames;
				}else{
					areaDisplay += ','+areaNames;
				}

				if(publishAreaIds == '' || publishAreaIds == undefined){
					publishAreaIds = areaIds;
				}else{
					publishAreaIds += ',' + areaIds;
				}
				$("#packageArea").attr('value', areaDisplay);
				$("#publishArea").attr('value', publishAreaIds);

		},
		error : function(errorMsg) {// 发生错误时消息处理
			$.messager.alert('错误信息','加载发布地区出错!','error');
		}
	});
}

function loadPubChannel(packageID){

	var param = "packageId="+packageId;
	$.ajax({
		type:"POST",
		url:"../json/getPackage2Channel.action?"+param,
		dataType:"json",
		async:false,
		success:function(data){

			 var channelIds='';
			 var channelNames='';
			 var publishchannelJSON = new Array();
			 var a = data;
				for(var i = 0; i < a.length; i++){
					channelIds += a[i].channelId;
					publishchannelJSON.push(a[i]);
					channelNames += a[i].name;
					if(i != (a.length-1)){
						channelIds += ',';
						channelNames += ',';
					}
				}
				var channelDisplay = $("#packageChannel").val();

				if(channelDisplay == '' ||channelDisplay == undefined){
					channelDisplay = channelNames;
				}else{
					channelDisplay += ','+channelNames;
				}
				$("#packageChannel").attr('value', channelDisplay);
				$("#publishChannel").val(JSON.stringify(publishchannelJSON));
		},
		error : function(errorMsg) {// 发生错误时消息处理
			alert(errorMsg);
			$.messager.alert('错误信息','加载发布渠道出错!','error');
		}
	});


}


function editArea(){
	$("#areaEdit").window('open');
	$("#areaEdit").window('refresh','selectArea.jsp?packageAreaId='+packageAreaId);
}

function editChannel(){
	var publishAreaId=packageAreaId;
	if(publishAreaId==""){
		$.messager.alert('提示信息','请先选择适用地区!','info');
		return;
	}
	$("#channelEdit").window('open');
	$("#channelEdit").window('refresh','selectChannel.jsp?publishAreaId='+publishAreaId);

}


function publishConfirm(){
	var areaIds= $("#publishArea").val();
	var row = $("#packageList").datagrid('getSelected');
	var packageId = row.packageId;
	var packageName= row.name;
	var param = "objId="+packageId+"&areaId="+g_areaId+"&staffId="+g_staffId;

		ajaxLoader.action.ajaxLoaderDivEffect(TIPS.publishing);
		
		//1.先校验包
		var response = $.ajax({
			type:"POST",
			url:"../json/checkPackageById.action",
			dataType:"json",
			async:false,
			data:{packId:packageId}
		});
		
		ajaxLoader.action.ajaxLoaderDivClose();
		
		var checkResult = JSON.parse(response.responseText);
		if(checkResult.result == "true") {
			if (checkResult.promptMsg && checkResult.promptMsg != "") {
				var msg = checkResult.promptMsg+"<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				var msg = msg+"【确定继续发布吗？】";
				$.messager.confirm("提示信息", msg, function (data) {
		            if (data) {
		                //2.校验成功之后发布
		            	ajaxPublish(param);
		            }
	        	});
			} else {
				 	  //2.校验成功之后发布
		         	   ajaxPublish(param);
			}
		} else {
			var msg = "【发布失败】:\n"+checkResult.errMsg;
			if (checkResult.promptMsg && checkResult.promptMsg != "") {
				msg += "\n【提示信息】：\n"+checkResult.promptMsg;
			}
			alert(msg);
		}
		
 	$("#publishwin").window('close');

}

function ajaxPublish(param) {
	$.ajax({
		type:"POST",
		async: false,
		url:"../json/PublishPackage.action?"+param,
		dataType:"json",

		data:{"areaIds":$("#publishArea").val(),"channels":encodeURIComponent($("#publishChannel").val())},
		success:function(data){
				if(data.result == true) {
					var msg = "【发布成功】";
					alert(msg);
				} else {
					var msg = "【发布失败】";
					alert(msg);
				}
		
				$("#publishwin").window('close');
		},
		error : function(errorMsg) {// 发生错误时消息处理
			$.messager.alert('错误信息','发布出错!','error');
			ajaxLoader.action.ajaxLoaderDivClose();
		}
	});
}

function publishCancle(){
	$("#publishwin").window('close');
}


